<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\api\home;
use app\common\controller\AuthCheck;
use think\facade\Cache;
use util\Tree;
/**
 * 用户公开控制器，不经过权限认证
 * @package app\user\admin
 */
class Baidu extends AuthCheck
{   
    /**
     * 视频统计
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function video_sum(){
        ob_end_clean();

        $data = [
            'status'    => 0,
            'msg'       => "",
            'data'      => model('video/VideoPlay') -> where([['play_type', '>', 0],['father_play_id' , '=', 0]]) ->count()
        ];

        return json($data);
    }

    /**
     * 短视频总数
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function video_duan_sum(){
        ob_end_clean();

        $data = [
            'status'    => 0,
            'msg'       => "",
            'data'      => model('video/VideoPlay')->where([['play_type', '=', 1],['father_play_id' , '=', 0]]) ->count()
        ];

        return json($data);
    }

    /**
     * 完整总数
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function video_chang_sum(){
        ob_end_clean();

        $data = [
            'status'    => 0,
            'msg'       => "",
            'data'      => model('video/VideoPlay')->where('play_type = 2') ->count()
        ];

        return json($data);
    }

    /**
     * 今日新增总数
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function video_today_sum(){
        ob_end_clean();
        $data = [
            'status'    => 0,
            'msg'       => "",
            'data'      => model('video/VideoPlay') -> where([['play_type', '>', 0],['father_play_id' , '=', 0]]) ->whereTime('create_time', 'between', [date('Y-m-d'), date('Y-m-d 23:59:59')]) ->count()
        ];

        return json($data);
    }

    /**
     * 视频分类占比
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function video_class_sum(){
        ob_end_clean();
        //查询一级分类
        $map = [];
        $map['pid'] = 0;
        $list = model('video/VideoPlayClass') -> where($map) -> order('sort asc,id asc') -> select();

        $list_all = model('video/VideoPlayClass') -> order('sort asc,id asc') -> select();
        foreach ($list as $key => $value) {
            $a = Tree::getChildsId($list_all , $value['id']);
            $map = [];
            $map[] = ['video_classid', 'in', $a];
            $map[] = ['father_play_id', '=', 0];
            $map[] = ['play_type', '>', 0];
            $rel[] = [
                'name' => $value['title'],
                'value' => model('video/VideoPlay')->where($map) ->count()
            ];
        }
        

        $data = [
            'status'    => 0,
            'msg'       => "",
            'data'      => $rel
        ];

        return json($data);
    }

    /**
     * 视频统计
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function video_count(){
        ob_end_clean();
        $now_time = time();
        $today_start_timer = strtotime( date("Y-m-d") );//今日开始时间
        $today_end_timer = strtotime( date("Y-m-d 23:59:59") );//今日结束时间
        $day = 10;
        for ($i=0; $i < ($day+1); $i++) { 
            $start_time = $today_start_timer - ($day-$i)*60*60*24;
            $end_time = $today_end_timer - ($day-$i)*60*60*24;
          
            $categories[] = date("Y-m-d", $start_time);
            $map = [];
            $map['play_type'] = 1;//短视频
            $map['father_play_id'] = 0;//短视频
            $duanshipin[] = model('video/VideoPlay')->where($map)->whereTime('create_time', 'between', [date('Y-m-d', $start_time), date('Y-m-d 23:59:59', $end_time)]) -> count();
            $map = [];
            $map['play_type'] = 2;//长视频
            $changshipin[] = model('video/VideoPlay')->where($map)->whereTime('create_time', 'between', [date('Y-m-d', $start_time), date('Y-m-d 23:59:59', $end_time)]) -> count();    
        }

        $data = [
            'status' => 0,
            'msg' => "",
            'data' =>[
                'categories' => $categories,
                'series'     => [
                    [
                        'name' => '短视频',
                        'data' => $duanshipin,
                    ],
                    [
                        'name' => '长视频',
                        'data' => $changshipin,
                    ],
                ]
             ],
        ];

        return json($data);
    }

    /**
     * 用户统计
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function user_count(){
        ob_end_clean();
        $now_time = time();
        $today_start_timer = strtotime( date("Y-m-d") );//今日开始时间
        $today_end_timer = strtotime( date("Y-m-d 23:59:59") );//今日结束时间
        $day = 10;
        for ($i=0; $i < ($day+1); $i++) { 
            $start_time = $today_start_timer - ($day-$i)*60*60*24;
            $end_time = $today_end_timer - ($day-$i)*60*60*24;
          
            $categories[] = date("Y-m-d", $start_time);
            $map = [];
            $user[] = model('user_center/Member', 'model\user')->where($map)->whereTime('create_time', 'between', [date('Y-m-d', $start_time), date('Y-m-d 23:59:59', $end_time)]) -> count();
            $user_sum[] = model('user_center/Member', 'model\user')->where($map)->whereTime('create_time', '<', date('Y-m-d 23:59:59', $end_time)) -> count();
        }
     
        $data = [
            'status' => 0,
            'msg' => "",
            'data' =>[
                'categories' => $categories,
                'series'     => [
                    [
                        'name' => '当日注册数',
                        'data' => $user,
                    ],
                    [
                        'name' => '用户总量',
                        'data' => $user_sum,
                    ],
                ]
             ],
        ];

        return json($data);
    }
	
	/**
     * 视频排行
     * @author shiqiang
     * @return mixed
     */
    public function video_ranking(){
        ob_end_clean();
        
		$list = model('video/VideoPlayWatchcount', 'model') -> field('play_id,count(id) as play_num,SUM(seconds) as seconds') ->group('play_id') -> order('play_num desc, seconds desc') -> limit(20) -> select();//播放次数/播放总时长
		
		$rows = [];
		foreach($list as $key => $value){
			$info = model('video/VideoPlay', 'model') -> getPlayinfoByPlayid($value['play_id']);
			if($info['code'] < 0)continue;
			$view_num = model('video/VideoPlayWatchcount', 'model') -> where('play_id', $value['play_id']) ->group('uid') -> count();//观看人数
			$average_play_time 				= ceil($value['seconds'] / $view_num);//平均播放时长
			$average_play_time_percentage 	= $average_play_time / $value['seconds'] * 100;//平均播放时长百分比
			$average_play_time_percentage 	= sprintf("%01.2f", $average_play_time_percentage);
			
			$data['play_id'] 						= $value['play_id'];	
			$data['title'] 						= $info['data']['main_title'];	
			$data['play_num'] 					= $value['play_num'] . '/' . $view_num;	
			$data['average_play_time'] 			= secToTime($average_play_time) . '/' . $average_play_time_percentage . '%';	
			
			$rows[] = $data;
			
		}
        $data = [
            'status' => 0,
            'msg' => "",
            'data' =>[
                'columns' => [
					['name' => '视频名称', 'id' => 'title', 'autoWrap' => true, 'width' => '160px'],
					['name' => '播放量/人次', 'id' => 'play_num', 'autoWrap' => true],
					['name' => '平均时长/百分比', 'id' => 'average_play_time', 'autoWrap' => true],
				],
				'rows' => $rows,
             ],
        ];
        return json($data);
    }
	
}
